package com.android.clinic.doctors.app.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

/**
 * Created by cheng on 2018/6/26.
 */
public abstract class BaseLazyLoadFragment extends BaseFragment {

    // 当前Fragment 是否可见
    protected boolean isVisible = false;
    // 是否加载过数据
    protected boolean isLoadData = false;

    // 标识view 是否初始化完成
    protected boolean isViewInit = false;

    @Override
    protected int getLayoutResId() {
        return getLayoutResId();
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.isViewInit = true;
        if(getUserVisibleHint()) {
            prepareLoadData(false);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisible = isVisibleToUser;
        prepareLoadData(false);
    }

    public void prepareLoadData(boolean forceLoad) {
        if (isViewInit && isVisible && (!isLoadData || forceLoad)) {
            lazyLoadData();
            isLoadData = true;
        }
    }

    protected abstract void lazyLoadData();
}
